package com.yazif.vqa.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yazif.vqa.api.wechat.domain.TextMessage;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class HttpUtils {

    public static JSONObject getMethod(String url) {

        StringBuilder entityStringBuilder = new StringBuilder();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity httpEntity = httpResponse.getEntity();
            try {
                String jsonString = EntityUtils.toString(httpEntity);
                return JSON.parseObject(jsonString);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static JSONObject postMethod(String url, String value){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        StringEntity entity = new StringEntity(value, "utf-8");
        entity.setContentEncoding("UTF-8");
        httpPost.setEntity(entity);
        try {
            CloseableHttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() ==  HttpStatus.SC_OK){
                String jsonString = EntityUtils.toString(response.getEntity());
                return JSON.parseObject(jsonString);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=7_LJUI7xYjig1Cuju-5HxkOKe7fHiCEUTsnPxNb1p3lD9jTf6GeyryyYn4QV6DDa9clZmuTSDN6nJskSbBpLHm7wRk6laiNto9t1XdyhNGmdoqE7Slwn6uKZc8II-CIMn92ZawpdCombyVbsElEYDcAJANHM";
        // JSONObject jsonObject = HttpUtils.getMethod(url);

        // System.out.println(jsonObject.toJSONString());

        TextMessage textMessage = new TextMessage();
        textMessage.setMsgtype("text");
        textMessage.setTouser("oGe4-windCH3KYxSAIRKuRzXagGc");
        textMessage.setText("hello world");

        HttpUtils.postMethod(url, JSON.toJSONString(textMessage));
        // System.out.println(JSON.toJSONString(textMessage));
    }
}
